home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Turbo Pascal V7 / TVFM.ZIP / GLOBALS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-11-03  |  11.9 KB  |  515 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Vision File Manager Demo               }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. {$X+,V-}
  9.  
  10. unit Globals;
  11.  
  12. interface
  13.  
  14. uses Objects, Drivers, App, Views, Menus, Dialogs, Dos, DragDrop;
  15.  
  16. type
  17.  
  18.   TConfigRec = record
  19.     FileMask: string[12];
  20.     ShowHidden: Word;
  21.     SortField: Word;
  22.     SortDir: Word;
  23.     DisplayCase: Word;
  24.     DisplayFields: Word;
  25.     Video: Word;
  26.   end;
  27.  
  28. { Event.InfoPtr points to a TScanInfo record if the when cmScanComplete
  29.   is broadcast }
  30.  
  31.   PScanInfo = ^TScanInfo;
  32.   TScanInfo = record
  33.     ScanCount: LongInt;
  34.     ScanBytes: LongInt;
  35.   end;
  36.  
  37.   PTextCollection = ^TTextCollection;
  38.   TTextCollection = object(TCollection)
  39.     procedure FreeItem(Item: pointer); virtual;
  40.   end;
  41.  
  42.   PProtectedStream = ^TProtectedStream;
  43.   TProtectedStream = object(TBufStream)
  44.     procedure Error(Code, Info: Integer); virtual;
  45.   end;
  46.  
  47.   { THCStatusLine is a help context sensitive status line }
  48.  
  49.   PHCStatusLine = ^THCStatusLine;
  50.   THCStatusLine = object(TStatusLine)
  51.     function Hint(AHelpCtx: Word): String; virtual;
  52.   end;
  53.  
  54.   { record used to identify a file by name only }
  55.   PFileNameRec = ^TFileNameRec;
  56.   TFileNameRec = record
  57.     Dir: DirStr;
  58.     Name: NameStr;
  59.     Ext: ExtStr;
  60.   end;
  61.  
  62.   { represents a single file in a file list }
  63.   PFileRec = ^TFileRec;
  64.   TFileRec = object(TObject)
  65.     Tagged: Boolean;
  66.     Name: NameStr;
  67.     Ext: ExtStr;
  68.     Attr: Byte;
  69.     Size: Longint;
  70.     Time: Longint;
  71.     constructor Init(const S: SearchRec);
  72.     procedure Toggle;
  73.   end;
  74.  
  75.   { moving view while files are being dragged }
  76.   PFileMover = ^TFileMover;
  77.   TFileMover = object(TMover)
  78.     procedure Draw; virtual;
  79.   end;
  80.  
  81.   { sorted collection that sorts according to the ConfigRec settings. }
  82.   PFileList = ^TFileList;
  83.   TFileList = object(TSortedCollection)
  84.     function Compare(Key1, Key2: Pointer): Integer; virtual;
  85.     procedure ReOrder;
  86.   end;
  87.  
  88.   TSortFunc = function(P1, P2: PFileRec): Integer;
  89.  
  90.   { dialog to handle file renaming }
  91.   PRenameDialog = ^TRenameDialog;
  92.   TRenameDialog = object(TDialog)
  93.     TheName: PathStr;
  94.     NewName: PathStr;
  95.     constructor Init(const FileName:PathStr);
  96.     function Valid(Command: Word): Boolean; virtual;
  97.   end;
  98.  
  99.   { dialog to handle changing file attributes }
  100.   PAttrDialog = ^TAttrDialog;
  101.   TAttrDialog = object(TDialog)
  102.     TheName: PathStr;
  103.     NewAttr: Word;
  104.     constructor Init(const FileName:PathStr);
  105.     function Valid(Command: Word): Boolean; virtual;
  106.   end;
  107.  
  108.   { TDeviceRec holds a single redirected device (net drives) }
  109.   PDeviceRec = ^TDeviceRec;
  110.   TDeviceRec = record
  111.     LocalName: Char;
  112.     NetworkName: PString;
  113.   end;
  114.  
  115.   { TDeviceCollection is a collection of TDeviceRecs }
  116.   PDeviceCollection = ^TDeviceCollection;
  117.   TDeviceCollection = object(TCollection)
  118.     procedure FreeItem(Item: Pointer); virtual;
  119.   end;
  120.  
  121. procedure RegisterGlobals;
  122. function WaitDialog(const Msg: String) : PDialog;
  123.  
  124. var
  125.   RezFile: TResourceFile;
  126.   RezStream: PStream;
  127.   RezStrings: PStringList;
  128.  
  129. const
  130.  
  131.   ConfigRec: TConfigRec =
  132.     (FileMask:'*.*'; ShowHidden:$00; SortField:$00; SortDir:$00;
  133.      DisplayCase:$00; DisplayFields:$FF; Video:0);
  134.  
  135.   ConfirmDelete: Boolean = True;
  136.   Viewer: PathStr = '';
  137.  
  138.   EXEName = 'TVFM.EXE';
  139.   CFGExt  = '.CFG';
  140.   TagChar = #251;
  141.  
  142.   UnwantedFiles: Word = VolumeID or Directory or SysFile or Hidden;
  143.  
  144. implementation
  145.  
  146. uses MsgBox, FileCopy, Equ;
  147.  
  148. const
  149.   RHCStatusLine : TStreamRec = (
  150.     ObjType : 100;
  151.     VmtLink : Ofs(TypeOf(THCStatusLine)^);
  152.     Load    : @THCStatusLine.Load;
  153.     Store   : @THCStatusLine.Store
  154.   );
  155.  
  156.  
  157. { ----------- General Purpose Routines -------------------- }
  158.  
  159. procedure RegisterGlobals;
  160. begin
  161.   RegisterType(RHCStatusLine);
  162. end;
  163.  
  164. function WaitDialog(const Msg: String) : PDialog;
  165. var
  166.   R: TRect;
  167.   D: PDialog;
  168.   Width: Integer;
  169.   XPos: Integer;
  170. begin
  171.   if Length(Msg) > 40 then Width := Length(Msg) + 4
  172.   else Width := 40;
  173.   XPos := (Width div 2) - (Length(Msg) div 2) - 1;
  174.  
  175.   R.Assign(0, 0, Width, 7);
  176.   D := New(PDialog, Init(R, RezStrings^.Get(sPleaseWait)));
  177.   with D^ do
  178.   begin
  179.     Options := Options or ofCentered;
  180.     Flags := Flags and (not wfClose) and (not wfMove);
  181.     R.Assign(XPos, 3, XPos+Length(Msg)+1, 4);
  182.     Insert(New(PStaticText,Init(R, Msg)));
  183.   end;
  184.   WaitDialog := D;
  185. end;
  186.  
  187. { TTextCollection }
  188. procedure TTextCollection.FreeItem(Item: pointer);
  189. begin
  190.   DisposeStr(Item);
  191. end;
  192.  
  193.  
  194. { TProtectedStream }
  195.  
  196. procedure TProtectedStream.Error(Code, Info: Integer);
  197. begin
  198.   Writeln('Error in stream: Code = ', Code, ' Info = ', Info);
  199.   Halt(1);
  200. end;
  201.  
  202.  
  203. { THCStatusLine }
  204.  
  205. function THCStatusLine.Hint(AHelpCtx: Word) :String;
  206. begin
  207.   Hint := RezStrings^.Get(AHelpCtx);
  208. end;
  209.  
  210. { TFileRec }
  211.  
  212. constructor TFileRec.Init(const S: SearchRec);
  213. var
  214.   T: PathStr;
  215. begin
  216.   inherited Init;
  217.   Tagged := False;
  218.   FSplit(S.Name, T, Name, Ext);
  219.  
  220.   { fix up directory names without extensions }
  221.   if (S.Attr and Directory <> 0) and (Name = '') then
  222.   begin
  223.     Name := Ext;
  224.     Ext := '';
  225.   end;
  226.   Attr := S.Attr;
  227.   Size := S.Size;
  228.   Time := S.Time;
  229. end;
  230.  
  231. procedure TFileRec.Toggle;
  232. begin
  233.   Tagged := not Tagged;
  234. end;
  235.  
  236.  
  237. { Sort functions for TFileList }
  238.  
  239. function SortByName(P1, P2: PFileRec): Integer; far;
  240. begin
  241.   if P1^.Name < P2^.Name then SortByName := -1
  242.   else if P1^.Name > P2^.Name then SortByName := 1
  243.   else SortByName := 0;
  244. end;
  245.  
  246. function SortByExt(P1, P2: PFileRec): Integer; far;
  247. begin
  248.   if P1^.Ext < P2^.Ext then SortByExt := -1
  249.   else if P1^.Ext > P2^.Ext then SortByExt := 1
  250.   else SortByExt := 0;
  251. end;
  252.  
  253. function SortBySize(P1, P2: PFileRec): Integer; far;
  254. begin
  255.   if P1^.Size < P2^.Size then SortBySize := -1
  256.   else if P1^.Size > P2^.Size then SortBySize := 1
  257.   else SortBySize := 0;
  258. end;
  259.  
  260. function SortByTime(P1, P2: PFileRec): Integer; far;
  261. begin
  262.   if P1^.Time < P2^.Time then SortByTime := -1
  263.   else if P1^.Time > P2^.Time then SortByTime := 1
  264.   else SortByTime := 0;
  265. end;
  266.  
  267. { TFileMover }
  268. procedure TFileMover.Draw;
  269. var
  270.   B: TDrawBuffer;
  271.   C: Word;
  272.   F: PFileRec;
  273. begin
  274.   C := GetColor(1);
  275.   { always draw at least the first entry in the collection }
  276.   F := Items^.At(0);
  277.   MoveChar(B, #32, C, Size.X);
  278.   MoveStr(B, F^.Name + F^.Ext, C);
  279.   WriteLine(0,0,Size.X,1,B);
  280.  
  281.   if Items^.Count > 1 then
  282.   begin
  283.     F := Items^.At(Items^.Count - 1);          { last item in list }
  284.     MoveChar(B, #32, C, Size.X);
  285.     MoveStr(B, F^.Name + F^.Ext, C);
  286.     if Items^.Count > 2 then
  287.     begin
  288.       WriteLine(0,2,Size.X,1,B);
  289.       if Items^.Count = 3 then
  290.       begin
  291.         F := Items^.At(1);
  292.         MoveChar(B, #32, C, Size.X);
  293.         MoveStr(B, F^.Name + F^.Ext, C);
  294.       end
  295.       else
  296.       begin
  297.         MoveChar(B, #32, C, Size.X);
  298.         MoveChar(B[4], #250, C, 4);
  299.       end;
  300.       WriteLine(0,1,Size.X,1,B);
  301.     end
  302.     else
  303.       WriteLine(0,1,Size.X,1,B);
  304.   end;
  305. end;
  306.  
  307. { TFileList }
  308.  
  309. function TFileList.Compare(Key1, Key2: Pointer): Integer;
  310. const
  311.   Sorts : array[0..3] of TSortFunc =
  312.    (SortByName, SortByExt, SortBySize, SortByTime);
  313. var
  314.   Result: Integer;
  315.   I: Integer;
  316. begin
  317.  
  318.   if Key2 = nil then
  319.   begin
  320.     Compare := 0;
  321.     Exit;
  322.   end;
  323.  
  324.   Result := Sorts[ConfigRec.SortField](Key1, Key2);
  325.   I := 0;
  326.   while (Result = 0) and (I <= 3) do
  327.   begin
  328.     Result := Sorts[I](Key1, Key2);
  329.     Inc(I);
  330.   end;
  331.  
  332.   { if the sort is descending, then reverse the Result variable }
  333.   if (ConfigRec.SortDir <> 0) and (Result <> 0) then
  334.     Result := Result * -1;
  335.  
  336.   Compare := Result;
  337. end;
  338.  
  339. procedure TFileList.ReOrder;
  340.  
  341. procedure Sort(l, r: Integer);
  342. var
  343.   i, j: Integer;
  344.   x, p: Pointer;
  345. begin
  346.   repeat
  347.     i := l; j := r;
  348.     x := KeyOf(Items^[(l + r) div 2]);
  349.     repeat
  350.       while Compare(KeyOf(Items^[i]), x) = -1 do Inc(i);
  351.       while Compare(x, KeyOf(Items^[j])) = -1 do Dec(j);
  352.       if i <= j then
  353.       begin
  354.     if i < j then
  355.     begin
  356.       p := Items^[i];
  357.       Items^[i] := Items^[j];
  358.       Items^[j] := p;
  359.     end;
  360.     Inc(i); Dec(j);
  361.       end;
  362.     until i > j;
  363.     if l < j then Sort(l, j);
  364.     l := i;
  365.   until l >= r;
  366. end;
  367.  
  368. begin
  369.   if Count > 1 then Sort(0, Count - 1);
  370. end;
  371.  
  372.  
  373. { TRenameDialog }
  374. constructor TRenameDialog.Init(const FileName: PathStr);
  375. var
  376.   R: TRect;
  377.   P: PView;
  378.   D: DirStr;
  379.   N: NameStr;
  380.   E: ExtStr;
  381. begin
  382.   R.Assign(0,0,40,7);
  383.   inherited Init(R, 'Rename File');
  384.   Options := Options or ofCentered;
  385.  
  386.   TheName := FileName;
  387.   FSplit(TheName, D, N, E);
  388.   D := N + E;
  389.   R.Assign(2,2,18,3);
  390.   Insert(New(PLabel, Init(R, '~' + D + '~ to ', nil)));
  391.   R.Assign(19,2,33,3);
  392.   Insert(New(PInputLine, Init(R, 12)));
  393.   R.Assign(4,4,16,6);
  394.   Insert(New(PButton, Init(R, '~O~K', cmOK, bfDefault)));
  395.   R.Move(16,0);
  396.   Insert(New(PButton, Init(R, '~C~ancel', cmCancel, bfNormal)));
  397.   SelectNext(False);
  398.   D := '';
  399.   SetData(D);
  400. end;
  401.  
  402. function TRenameDialog.Valid(Command: Word): Boolean;
  403. var
  404.   L: Longint;
  405.   TheFile: File;
  406.   D: DirStr;
  407.   N: NameStr;
  408.   E: ExtStr;
  409.   I: Integer;
  410. begin
  411.   Valid := True;
  412.   if (Command = cmCancel) or (Command = cmValid) then Exit;
  413.   GetData(NewName);
  414.   for I:= 1 to Length(NewName) do NewName[I] := UpCase(NewName[I]);
  415.   FSplit(TheName, D, N, E);
  416.  
  417.   { check for duplicate name }
  418.   if D + NewName = TheName then
  419.   begin
  420.     MessageBox(RezStrings^.Get(sSameNameErr), nil, mfError+mfOKButton);
  421.     Valid := False;
  422.     Exit;
  423.   end;
  424.   Assign(TheFile, TheName);
  425.   {$I-}
  426.   Rename(TheFile, D + NewName);
  427.   {$I+}
  428.   L := IOResult;
  429.   if L <> 0 then
  430.   begin
  431.     MessageBox(RezStrings^.Get(sRenameErr), @L, mfError+mfOKButton);
  432.     Valid := False;
  433.   end;
  434. end;
  435.  
  436. { TAttrDialog }
  437. constructor TAttrDialog.Init(const FileName:PathStr);
  438. var
  439.   R: TRect;
  440.   P: PView;
  441.   Attr: Word;
  442.   XFer: Word;
  443.   TheFile: File;
  444. begin
  445.   R.Assign(0,0,40,12);
  446.   inherited Init(R, 'Change Attributes');
  447.   Options := Options or ofCentered;
  448.  
  449.   TheName := FileName;
  450.   Assign(TheFile, TheName);
  451.   GetFAttr(TheFile, Attr);
  452.   if DosError <> 0 then Fail;
  453.  
  454.   R.Assign(0,2,Length(FileName),3);
  455.   P:=New(PStaticText, Init(R, FileName));
  456.   P^.Options := P^.Options or ofCenterX;
  457.   Insert(P);
  458.   R.Assign(0,4,15,8);
  459.   P := New(PCheckBoxes, Init(R, NewSItem('~A~rchive',
  460.                                 NewSItem('~R~ead-Only',
  461.                                 NewSItem('~S~ystem',
  462.                                 NewSItem('~H~idden',
  463.                                 nil))))));
  464.   P^.Options := P^.Options or ofCenterX;
  465.   Insert(P);
  466.   R.Assign(4,9,16,11);
  467.   Insert(New(PButton, Init(R, '~O~K', cmOK, bfDefault)));
  468.   R.Move(16,0);
  469.   Insert(New(PButton, Init(R, '~C~ancel', cmCancel, bfNormal)));
  470.   SelectNext(False);
  471.   XFer := 0;
  472.   if Attr and Archive <> 0 then XFer := $01;
  473.   if Attr and ReadOnly <> 0 then XFer := XFer or $02;
  474.   if Attr and SysFile <> 0 then XFer := XFer or $04;
  475.   if Attr and Hidden <> 0 then XFer := XFer or $08;
  476.   SetData(XFer);
  477. end;
  478.  
  479. function TAttrDialog.Valid(Command: Word): Boolean;
  480. var
  481.   XFer : Word;
  482.   L: array[0..1] of Longint;
  483.   TheFile: File;
  484. begin
  485.   Valid := True;
  486.   if (Command = cmCancel) or (Command = cmValid) then Exit;
  487.   GetData(XFer);
  488.   NewAttr := 0;
  489.   if XFer and $01 <> 0 then NewAttr := Archive;
  490.   if XFer and $02 <> 0 then NewAttr := NewAttr or ReadOnly;
  491.   if XFer and $04 <> 0 then NewAttr := NewAttr or SysFile;
  492.   if XFer and $08 <> 0 then NewAttr := NewAttr or Hidden;
  493.   Assign(TheFile, TheName);
  494.   SetFAttr(TheFile, NewAttr);
  495.   if DosError <> 0 then
  496.   begin
  497.     L[0] := DosError;
  498.     L[1] := Longint(@TheName);
  499.     MessageBox(RezStrings^.Get(sSetAttrErr), @L, mfError+mfOKButton);
  500.     Valid := False;
  501.   end;
  502. end;
  503.  
  504. { TDeviceCollection }
  505. procedure TDeviceCollection.FreeItem(Item: Pointer);
  506. var
  507.   DeviceRec : PDeviceRec absolute Item;
  508. begin
  509.   DisposeStr(DeviceRec^.NetworkName);
  510.   Dispose(DeviceRec);
  511. end;
  512.  
  513.  
  514. end. { unit }
  515.